package com.developer.sqliteexample.activity;

import java.util.List;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.developer.sqliteexample.R;
import com.developer.sqliteexample.model.DbContext;
import com.developer.sqliteexample.model.entity.Employee;
import com.developer.sqliteexample.util.Util;

public class MainActivity extends Activity {

	private static final String DELIMITER = ";";

	private EditText txtFirsName;
	private EditText txtLastName;
	private Button btnInsert;
	private Button btnGetAll;
	private EditText txtEmployeeId;
	private Button btnFindById;
	private Button btnDelete;
	private Button btnUpdateName;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		initializeComponents();
		registerEvents();
	}

	private void initializeComponents() {
		txtFirsName = (EditText) findViewById(R.id.txt_first_name);
		txtLastName = (EditText) findViewById(R.id.txt_last_name);
		btnInsert = (Button) findViewById(R.id.btn_insert);
		btnGetAll = (Button) findViewById(R.id.btn_get_all);
		txtEmployeeId = (EditText) findViewById(R.id.txt_employee_id);
		btnFindById = (Button) findViewById(R.id.btn_find_by_id);
		btnDelete = (Button) findViewById(R.id.btn_delete);
		btnUpdateName = (Button) findViewById(R.id.btn_update_name);
	}

	private void registerEvents() {
		btnInsert.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				Employee employee = new Employee();
				employee.setFirstName(txtFirsName.getText().toString());
				employee.setLastName(txtLastName.getText().toString());
				boolean result = DbContext.getInstance(MainActivity.this).insertEmployee(employee);
				Util.showMessage(MainActivity.this, result ? "Insert Success" : "Inser Failed!");
			}
		});
		btnGetAll.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				List<Employee> employees = DbContext.getInstance(MainActivity.this).getAllEmployees();
				Util.showMessage(MainActivity.this, "Record Count: " + employees.size());
				for (Employee employee : employees) {
					Util.showMessage(MainActivity.this, employee.toString());
				}
			}
		});
		btnFindById.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				long id = Long.valueOf(txtEmployeeId.getText().toString());
				Employee employee = DbContext.getInstance(MainActivity.this).findById(id);
				Util.showMessage(MainActivity.this, employee == null ? "Id not found" : employee.toString());
			}
		});
		btnDelete.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				long id = Long.valueOf(txtEmployeeId.getText().toString());
				boolean result = DbContext.getInstance(MainActivity.this).deleteEmployee(id);
				Util.showMessage(MainActivity.this, result ? "Employee Deleted Success" : "Employee Deletion Failed!");
			}
		});
		btnUpdateName.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				String inputValue = txtFirsName.getText().toString();
				String oldName = inputValue.substring(0, inputValue.indexOf(DELIMITER));
				String newName = inputValue.substring(inputValue.indexOf(DELIMITER) + 1);
				boolean result = DbContext.getInstance(MainActivity.this).updateEmployeeName(oldName, newName);
				Util.showMessage(MainActivity.this, result ? "Name Updated Succesfully" : "Name Update Failed!");
			}
		});
	}

}
